using Nemerle.Collections;

class Task033 : TaskBase
{
  override public SolveInt() : int
  {
    def facts = array(10);
    facts[0] = 1;
    def calcFact(n)
    {
      | 10 => {}
      | _ =>
        facts[n] = facts[n - 1] * n;
        calcFact(n + 1)
    }
    calcFact(1);
    
    def factSum(acc, n)
    {
      | (_, 0) => acc
      | _ => factSum(acc + facts[n % 10], n / 10)
    }
    def allSum(acc, n)
    {
      | (_, 10000000) => acc
      | _ => allSum(acc + if (n == factSum(0, n)) n else 0, n + 1)
    }
    allSum(0, 10)
  }
}
